package com.example.oss.config;

import com.example.oss.Interceptor.AccessLimitInterceptor;
import com.example.oss.Interceptor.RefreshLimitInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;




/**
 * 处理跨域问题
 *
 * @Author wang suo
 * @Date 2020/10/10 0010 21:37
 * @Version 1.0
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {



    @Autowired
    private AccessLimitInterceptor accessLimitInterceptor;

    @Autowired
    private RefreshLimitInterceptor refreshLimitInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        /*
         * 一小时内不需要再预检
         */
        registry.addMapping("/**")
                .allowedOrigins("/*")
                .allowCredentials(true)
                .allowedHeaders(CorsConfiguration.ALL)
                .allowedMethods(CorsConfiguration.ALL)
                .maxAge(3600);
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(refreshLimitInterceptor);
        registry.addInterceptor(accessLimitInterceptor);
    }
}